import { authApi } from './auth';
import { LoginRequest } from './types';

// 测试跨域配置和API调用
export const testApiConnection = async () => {
  console.log('开始测试API连接...');
  
  try {
    // 测试登录API
    const loginData: LoginRequest = {
      username: 'test',
      password: 'test123'
    };
    
    console.log('发送登录请求到:', '/api/auth/login');
    const result = await authApi.login(loginData);
    console.log('登录成功:', result);
    
    return result;
  } catch (error) {
    console.error('API测试失败:', error);
    throw error;
  }
};

// 测试网络连接
export const testNetworkConnection = async () => {
  try {
    const response = await fetch('/api/auth/login', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        username: 'test',
        password: 'test123'
      })
    });
    
    console.log('网络请求状态:', response.status);
    const data = await response.json();
    console.log('网络请求响应:', data);
    
    return data;
  } catch (error) {
    console.error('网络测试失败:', error);
    throw error;
  }
};

// 在浏览器控制台中运行测试
if (typeof window !== 'undefined') {
  (window as any).testApi = {
    testApiConnection,
    testNetworkConnection
  };
  
  console.log('API测试工具已加载，可以在控制台中使用:');
  console.log('- window.testApi.testApiConnection()');
  console.log('- window.testApi.testNetworkConnection()');
} 